package com.aternity.vbd.collect;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Daniel Orr
 */
@Repository
public class VbdUtilsDao {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public List<VbdLog> getVbdLogs() {
        final String sql =
                "select \n" +
                "   timestamp, log_level, message from aternity_log \n" +
                "where \n" +
                "   timestamp > sysdate -1/12 and message like 'vbd%' order by id desc";

        return jdbcTemplate.query(sql, new Object[]{}, new RowMapper<VbdLog>() {
            @Override
            public VbdLog mapRow(ResultSet rs, int rowNum) throws SQLException {
                VbdLog log = new VbdLog();
                log.setTimestamp(rs.getDate(1));
                log.setLevel(rs.getString(2));
                log.setMessage(rs.getString(3));
                return log;
            }
        });
    }
}
